/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.susx.inf.ianw.shareManagement.client.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import uk.ac.susx.inf.ianw.shareManagement.dto.PortfolioDetail;
import uk.ac.susx.inf.ianw.shareManagement.entities.ShareTransaction;
import uk.ac.susx.inf.ianw.shareManagement.services.impl.ShareBrokerService;
import uk.ac.susx.inf.ianw.shareManagement.services.impl.ShareTransactionService;

@ManagedBean
public class ShareManagementController {

    private List<ShareTransaction> shareTransactionList;
    
    @EJB
    private ShareTransactionService shareTransactionService;
    
    @EJB
    private ShareBrokerService shareBrokerService;
    
    private static final String[] companies = {"Google", "Apple", "Oracle", "Microsoft"};
    
    private List<PortfolioDetail> portfolioDetails;
    
    private String selectedCompany = "Google";
    private Integer selectedAmount;
    private String message;
    private Double currentPrice;
    
    public ShareManagementController() {
    }
    
    public String[] getCompanies(){
        return companies;
    }
    
    public Double getCurrentPrice(){
        try {
            currentPrice = shareBrokerService.getPrice(selectedCompany);
        } catch (Exception ex) {
            currentPrice = new Double(0);
        }
        return currentPrice;
    }
    
    public List<PortfolioDetail> getPortfolioDetails() throws Exception{
        portfolioDetails = new ArrayList<PortfolioDetail>();
        for(String company : companies){
            PortfolioDetail detail = shareBrokerService.getPortfolioDetail(company);
            if(detail.getAmount() > 0){
                portfolioDetails.add(detail);
            }
        }
        return portfolioDetails;
    }
    
    
    public List<ShareTransaction> getShareTransactionList() throws Exception{
        shareTransactionList = shareTransactionService.readList();
        return shareTransactionList;
    }
    
    public String deleteShareTransaction() throws Exception{
        FacesContext context = FacesContext.getCurrentInstance(); 
        Map map = context.getExternalContext().getRequestParameterMap(); 
        Long id = Long.parseLong(map.get("transactionId").toString());
        shareTransactionService.delete(id);
        return null;
    }
    
    public String buyShares() throws Exception{
        ShareTransaction transaction = new ShareTransaction();
        transaction.setAmount(selectedAmount);
        transaction.setCompany(selectedCompany);
        shareBrokerService.makePurchaseRequest(transaction);
        return null;
    }

    public String getSelectedCompany() {
        return selectedCompany;
    }

    public void setSelectedCompany(String selectedCompany) {
        this.selectedCompany = selectedCompany;
    }

    public Integer getSelectedAmount() {
        return selectedAmount;
    }

    public void setSelectedAmount(Integer selectedAmount) {
        this.selectedAmount = selectedAmount;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setCurrentPrice(Double currentPrice) {
        this.currentPrice = currentPrice;
    }
    
    
}
